﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Principal;
using System.Diagnostics;

namespace LiveWriterBackupWPF
{
    public static class WLMisc
    {
        public static OpenFileDialog CreateDialog()
        {
            OpenFileDialog result = new OpenFileDialog();
            result.DefaultExt = "wlwbackup";
            result.Filter = "Windows Live Writer Backups (*.wlwbackup)|*.wlwbackup";
            result.Title = "Choose your backup file";

            return result;
        }

        public static SaveFileDialog CreateSaveFile()
        {
            SaveFileDialog result = new SaveFileDialog();
            result.DefaultExt = "wlwbackup";
            result.Filter = "Windows Live Writer Backups (*.wlwbackup)|*.wlwbackup";
            result.Title = "Choose a name for your backup file";
            return result;
        }

        public static string AboutInfo
        {
            get
            {
                StringBuilder aboutInfo = new StringBuilder();
                aboutInfo.Append("Windows Live Writer Backup\n");
                aboutInfo.Append("Version: " + AssemblyInfo.AssemblyVersion + "\n");
                aboutInfo.Append("Copyright: " + AssemblyInfo.AssemblyCopyright + "\n");
                aboutInfo.Append(AssemblyInfo.AssemblyDescription);
                return aboutInfo.ToString();
            }
        }

        public static bool IsAnAdministrator()
        {
            WindowsIdentity identity = WindowsIdentity.GetCurrent();
            WindowsPrincipal principal = new WindowsPrincipal(identity);
            return principal.IsInRole(WindowsBuiltInRole.Administrator);
        }

        internal static void RestartElevated(string filename)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.UseShellExecute = true;
            startInfo.WorkingDirectory = Environment.CurrentDirectory;
            startInfo.FileName = Application.ExecutablePath;
            startInfo.Verb = "runas";
            startInfo.Arguments = filename;
            try
            {
                Process p = Process.Start(startInfo);
            }
            catch (System.ComponentModel.Win32Exception ex)
            {
                return; //If cancelled, do nothing
            }

            Application.Exit();
        }
    }    
}
